<?php namespace Phpcmf\Controllers;

class Guanzhu extends \Phpcmf\Home\Module
{

	// 关注
	public function guanzhu_panduan() {
		//get 参数
		/*$data = [
			'code' => 0,
			'data' => '未登录',
			'guanzhu'=> 0,
		];
		$this->_json('1', '未登录',$data);	*/			
		// 判断是否登录
		if(!$this->uid){
			$data = [
				'code' => 0,
				'data' => '未登录',
				'guanzhu'=> 0,
			];
			$this->_json('1', '未登录',$data);									
		} else {
			// 登录后
			$touid = (int)\Phpcmf\Service::L('input')->get('touid');
			$uid = (int)\Phpcmf\Service::L('input')->get('uid');
			// 检查是否关注
			$guanzhu_data = \Phpcmf\Service::M()->db->table('member_guanzhu')->where('touid', $touid)->where('uid', $this->uid)->countAllResults();
			if($guanzhu_data){
				$data_msg = "已经关注";
			} else {
				$data_msg = "没有关注";
			}
			$data = [
				'code' => 1,
				'data' => $data_msg,
				'guanzhu'=> $guanzhu_data,
			];
			$this->_json('1', $data_msg,$data);								


		}

	}
	// 关注
	public function guanzhu() {
		//post 参数
		//判断post 
		if (IS_POST) {
			$uid = (int)\Phpcmf\Service::L('input')->post('uid'); //用户id
			$touid = (int)\Phpcmf\Service::L('input')->post('touid'); // 圈子id
			$guanzhu_data = \Phpcmf\Service::M()->db->table('member_guanzhu')->where('uid', $this->uid)->where('touid', $touid)->countAllResults();
			if($this->uid == $touid){
				// 不能关注自己
				$this->_json('0', '不能关注自己');
			}
			if($guanzhu_data){
				// 取消关注
				\Phpcmf\Service::M()->db->table('member_guanzhu')->where('uid', $this->uid)->where('touid', $touid)->delete();
				$data = [
					'code' => 0,
					'data' => '取消关注',
					'guanzhu'=> 0,
				];
				$this->_json('1', '取消关注',$data);
			} else {
				// 关注
				$insert_data = [
					'uid' => $this->uid,
					'touid' => $touid,
					'tianjiashijian' => time(),
				];
				$insert_id = \Phpcmf\Service::M()->db->table('member_guanzhu')->insert($insert_data);
				if($insert_id){
					$data = [
						'code' => 1,
						'data' => '关注成功',
						'guanzhu'=> 1,
					];
				}
				$guanzhu_uid = dr_member_info($this->uid);
				if($guanzhu_uid['username']){
					$guanzhu_ming = $guanzhu_uid['username'];
				} elseif($guanzhu_uid['name']) {
					$guanzhu_ming = $guanzhu_uid['name'];
				}
				$dataduixin = [
					'catid'=> 17, //类型
					'title' => '关注', //标题
					'zhiding' => '', //置顶
					'content'=> $guanzhu_ming.'关注了你', //通知内容
					'uid'=> $this->uid, //发布人的uid
					'faxinren'=> $this->uid, //发布人的uid
					'shouxinren'=> (int)$touid,//收信人
					'tupian'=> '', //图片
					'shipin'=> '', //视频
					'yuyin'=> '', //语音
					'fujian'=> '', //附件
					'yidu'=> 0, //已读
					'quanziid'=> 0, //圈子ID
					'quantieid'=> 0, //圈帖ID
					'url' => '',
				];
				if($this->uid != (int)$touid){
					// 通知
					\Phpcmf\Service::M('auth', 'duixin')->addtongzhi($dataduixin);
				}

				$this->_json('1', '关注成功',$data);
			}

		} else {
			$this->_json('0', '无效提交');
		}
	}
}
